package com.kitkat.message.handler;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.kitkat.message.Message;

@Component
public class MessageHandler{
	private List<MessageSender> messageSenders = new ArrayList<MessageSender>();
	
	public synchronized void registerMessageSender(MessageSender sender){
		for(int i=0;i<this.messageSenders.size();i++){
			if(this.messageSenders.get(i).getClass().equals(sender.getClass())){
				return ;
			}
		}
		this.messageSenders.add(sender);
	}
	
	public void processMessage(Message message){
		for(int i=0;i<this.messageSenders.size();i++){
			MessageSender sender = this.messageSenders.get(i);
			if(sender.supports(message)){
				sender.doWithMessage(message);
				return ;
			}
		}
	}
}